package com.learn.xueer.ui.activity

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.Observer
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.contrarywind.interfaces.IPickerViewData
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseActivity
import com.learn.xueer.data.model.bean.AreaItem
import com.learn.xueer.databinding.ActivitySelectSchoolBinding
import com.learn.xueer.ui.adapter.LetterAdapter
import com.learn.xueer.ui.viewmodel.RequestLoginViewModel
import com.learn.xueer.ui.widgets.SideBar
import com.learn.xueer.ui.widgets.WheelView
import kotlinx.android.synthetic.main.activity_select_school.tv_area
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.view.afterTextChange
import me.hgj.jetpackmvvm.util.ActivityMessenger


/**
 * 选择所在学校
 */
class SelectSchoolActivity : BaseActivity<RequestLoginViewModel, ActivitySelectSchoolBinding>() {
    private val letterAdapter: LetterAdapter by lazy {
        LetterAdapter(arrayListOf()) {
            ActivityMessenger.finish(
                this@SelectSchoolActivity,
                "name" to it.name,
                "id" to it.id,
                "city" to it.city
            )
        }
    }
    private var areaData: List<List<List<AreaItem>>> = arrayListOf()
    private var cityData: List<List<AreaItem>> = arrayListOf()
    private var provinceData: List<AreaItem> = arrayListOf()
    var optionsPickerView: OptionsPickerView<AreaItem>? = null
    var city = "广东省/深圳市/宝安区"
    var name = ""
    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(mViewModel)
        optionsPickerView =
            OptionsPickerBuilder(this@SelectSchoolActivity) { option1, option2, option3, v ->
                city =
                    "${provinceData[option1].label}/${cityData[option1][option2].label}/${areaData[option1][option2][option3].label}"
                mViewModel.schoolListReq(city, name)
                tv_area.text = city.replace("/", " ")
            }.isRestoreItem(true)
                .setCancelColor(Color.parseColor("#FF8E8E8E"))
                .setSubmitColor(Color.parseColor("#FF0CB76C"))
                .setSubCalSize(16)
                .setContentTextSize(16)
                .build<AreaItem>()

        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()
        mViewModel.schoolListReq(city, name)
        mViewModel.areaReq {
            provinceData = it?.provinceData ?: arrayListOf()
            cityData = it?.cityData ?: arrayListOf()
            areaData = it?.areaData ?: arrayListOf()
            optionsPickerView?.setPicker(provinceData, cityData, areaData)
        }
        mDatabind.rvData.adapter = letterAdapter
        mDatabind.letterView.setOnTouchingLetterChangedListener(object :
            SideBar.OnChooseLetterChangedListener {
            override fun onChooseLetter(letter: String?) {
                var indexOfFirst = letterAdapter.data.indexOfFirst {
                    it.name == letter
                }
                LogUtils.e("$indexOfFirst $letter")
                mDatabind.rvData.smoothScrollToPosition(indexOfFirst)
            }

            override fun onNoChooseLetter() {

            }
        })
    }

    override fun createObserver() {

        mDatabind.etSchool.afterTextChange {
            name = it
            mViewModel.schoolListReq(city, name)
        }
        mViewModel.schoolResult.observe(this, Observer { resultState ->
            parseState(resultState, {
                var mLetters: Array<String?> = arrayOfNulls(it?.size ?: 0)
                it?.forEachIndexed { index, schoolResponse ->
                    mLetters[index] = schoolResponse.name
                }
                mDatabind.letterView.setLetters(mLetters)
                letterAdapter.setList(it)

            }, {
                ToastUtils.showShort(it.errorMsg)

            })
        })
    }

    inner class ProxyClick {

        fun back() {
            finish()
        }

        fun select() {
            if (areaData.isNotEmpty()) {
                optionsPickerView?.show()
            } else {
                showLoading()
                Handler().postDelayed({
                    runOnUiThread {
                        dismissLoading()
                        optionsPickerView?.show()
                    }
                }, 3000)
            }


        }


    }
}